package com.gz.moreaseManager.modules.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gz.moreaseManager.config.SelfUserDetails;
import com.gz.moreaseManager.core.base.BaseController;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.core.modal.HttpCode;
import com.gz.moreaseManager.core.modal.PageResult;
import com.gz.moreaseManager.modules.entity.Banner;
import com.gz.moreaseManager.modules.service.IBannerService;
import com.gz.moreaseManager.utils.DateUtil;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 轮播图信息 前端控制器
 * </p>
 *
 * @author wike
 * @since 2019-02-21
 */
@RestController
@RequestMapping("/banner")
public class BannerController extends BaseController {

    @Autowired
    private IBannerService bannerService;

    /**
     * 菜单管理界面
     *
     * @return 页面模板
     */
    @GetMapping("list")
    public ModelAndView list() {
        return new ModelAndView("modules/banner/list");
    }

    @GetMapping("listPage")
    public PageResult<Banner> findPageList(String bannerName, String bannerType,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
                                           @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                           Integer pageNum, Integer pageSize) {

        //分页
        Page paramsPage = new Page();
        paramsPage.setCurrent(pageNum);
        paramsPage.setSize(pageSize);
        //构建查询
        Wrapper wrapper = new EntityWrapper<Banner>();
        if (UtilTool.isNotBlank(bannerName))
            wrapper.like("banner_name", bannerName);
        if (UtilTool.isNotBlank(bannerType))
            wrapper.eq("banner_type", bannerType);
        if (UtilTool.isNotBlank(startDate)) {
            if ( UtilTool.isNotBlank( endDate ) && startDate.getTime() == endDate.getTime()) {
                wrapper.between("create_time", startDate, DateUtil.addDays(startDate, 1));
            } else {
                wrapper.between("create_time", startDate,
                        UtilTool.isNotBlank(endDate)?DateUtil.addDays(endDate,1):DateUtil.addDays(new Date(),1));
            }
        }
        if( UtilTool.isBlank(startDate) && UtilTool.isNotBlank( endDate )){
            wrapper.le("create_time",DateUtil.addDays(endDate,1));
        }
        //排序
        wrapper.orderBy("sort",true);
        wrapper.orderBy("create_time",false);

        Page<Banner> data = bannerService.selectPage(paramsPage, wrapper);
        return new PageResult(0, null, data.getTotal(), data.getRecords());
    }

    /**
     * 新增轮播图信息
     *
     * @param banner
     * @return
     */
    @PostMapping("/addBanner")
    public AjaxResult<Boolean> addBannerInfo(Banner banner,Authentication authentication) {

        if (banner == null
                || UtilTool.isBlank(banner.getBannerName())
                || UtilTool.isBlank(banner.getBannerType())
                || UtilTool.isBlank(banner.getBannerPosition()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        //除首页外都只有一条记录(门店效果轮播图)
        if( banner.getBannerType() !=1 && banner.getBannerType() != 6){
            Banner bannerOld = bannerService.findBannerByParams( banner.getBannerType(),banner.getBannerPosition());
            if( UtilTool.isNotBlank( bannerOld ))
                return AjaxResult.build(HttpCode.ARGS_ERROR, "轮播图类型已经存在!");
        } else if( banner.getBannerType() == 6 ){//门店效果轮播图
            Wrapper query = new EntityWrapper<Banner>();
            query.eq("banner_type",banner.getBannerType());
            List<Banner> list = bannerService.selectList(query);
            if( UtilTool.isNotBlank( list ) && list.size() ==6)
                return AjaxResult.build(HttpCode.ARGS_ERROR, "门店版图轮播图最多6条!");
        }
        //新增信息
        banner.setCreateTime(new Date());
        //默认不显示
        if (UtilTool.isBlank(banner.getStatus()))
            banner.setStatus(0);

        //创建人
        SelfUserDetails userDetails = (SelfUserDetails) authentication.getPrincipal();
        banner.setCreatePerson( userDetails.getUsername());
        boolean success = bannerService.insert(banner);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "新增轮播图信息失败!");
    }

    /**
     * 根据ID获取轮播图信息
     *
     * @param bannerId
     * @return
     */
    @GetMapping("findBanner")
    public AjaxResult<Banner> findBanner(Integer bannerId) {
        if (UtilTool.isBlank(bannerId))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Banner banner = bannerService.selectById(bannerId);
        return AjaxResult.success(banner);
    }

    /**
     * 新增轮播图信息
     *
     * @param banner
     * @return
     */
    @PostMapping("/editBanner")
    public AjaxResult<Boolean> editBannerInfo(Banner banner) {
        if (banner == null || UtilTool.isBlank(banner.getBannerId())
                || UtilTool.isBlank(banner.getBannerName()))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        //新增信息
        boolean success = bannerService.updateById(banner);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改轮播图信息失败!");
    }


    /**
     * 根据ID删除记录
     *
     * @param bannerId
     * @return
     */
    @PostMapping("delete")
    public AjaxResult<Boolean> delete(Integer bannerId) {

        if (UtilTool.isBlank(bannerId))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");

        Boolean success = bannerService.deleteById(bannerId);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "删除轮播图信息失败!");
    }

    /**
     * 修改状态
     * @param bannerId
     * @param status
     * @return
     */
    @PostMapping("setStatus")
    public AjaxResult<Boolean> setStatus( Integer bannerId,Integer status){
        if (UtilTool.isBlank(bannerId) || UtilTool.isBlank(status))
            return AjaxResult.build(HttpCode.ARGS_ERROR, "请求参数错误!");
        Banner banner = new Banner();
        banner.setBannerId( bannerId);
        banner.setStatus( status );
        Boolean success = bannerService.updateById( banner);
        return success ? AjaxResult.success() : AjaxResult.build(HttpCode.FAILED, "修改轮播图状态失败!");
    }

    /**
     * 根据轮播图位置获取信息
     * @param bannerType
     * @return
     */
    @GetMapping("findBannerByBannerType")
    public AjaxResult<Banner> findBannerByBannerType( Integer bannerType ,Integer bannerPosition){
        if( UtilTool.isNotBlank( bannerType )){
            Banner banner = bannerService.findBannerByParams(bannerType,bannerPosition);
            if( UtilTool.isNotBlank( banner ))
                return AjaxResult.success(banner );
        }
        return AjaxResult.build(HttpCode.FAILED, "获取轮播图信息失败!");
    }

    /**
     * 获取首页轮播图信息
     * @return
     */
    @GetMapping("findBannerIndexList")
    public AjaxResult<List<Banner>> findBannerIndexList(){
        List<Banner> bannerList = bannerService.findBannerIndexList();
        if( UtilTool.isNotBlank( bannerList ))
            return AjaxResult.success(bannerList );
        return AjaxResult.build(HttpCode.FAILED, "获取轮播图信息失败!");
    }

    /**
     * 获取首页轮播图信息
     * @return
     */
    @GetMapping("findBannerAdvertList")
    public AjaxResult<List<Banner>> findBannerAdvertList(){
        List<Banner> bannerList = bannerService.findBannerAdvertList();
        if( UtilTool.isNotBlank( bannerList ))
            return AjaxResult.success(bannerList );
        return AjaxResult.build(HttpCode.FAILED, "获取轮播图信息失败!");
    }

    /**
     * 获取门店版图轮播图信息
     * @return
     */
    @GetMapping("findBannerLayoutList")
    public AjaxResult<List<Banner>> findBannerLayoutList(){
        List<Banner> bannerList = bannerService.findBannerLayoutList();
        if( UtilTool.isNotBlank( bannerList ))
            return AjaxResult.success(bannerList );
        return AjaxResult.build(HttpCode.FAILED, "获取轮播图信息失败!");
    }

}
